/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lolbuilder.calculation;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import lolbuilder.Items.Interf.ItemsSuper;
import lolbuilder.build.Build;
import lolbuilder.mainClasses.AllItems;

/**
 *
 * @author maximveron
 */
public class LinearCompositionThread extends Thread{
    ArrayList<Build> dispoBuilds=new ArrayList<Build>();
    final Integer mutex=new Integer(0);
    boolean finished=false;
    public final static int HOWMUCH=3000000;

    @Override
    public void run() {
        HashMap<String,Integer> buildsFound=new HashMap<String, Integer>();
        AllItems.populate();
        int size=AllItems.items.size();
        int i=0;
        System.out.println(AllItems.items);
        for (ItemsSuper i0 : AllItems.items) {
            for (ItemsSuper i1 : AllItems.items) {
                for (ItemsSuper i2 : AllItems.items) {
                    for (ItemsSuper i3 : AllItems.items) {
                        for (ItemsSuper i4 : AllItems.items) {
                            for (ItemsSuper i5 : AllItems.items) {
                               ArrayList<String> ss= new ArrayList<String>();
                                
                               ss.add(i0.name);
                               ss.add(i1.name);
                               ss.add(i2.name);
                               ss.add(i3.name);
                               ss.add(i4.name);
                               ss.add(i5.name);
                               Collections.sort(ss);
                               String s="";
                               for (String s2 : ss) s+=s2;
                               if (buildsFound.get(s)==null) {
                                   buildsFound.put(s, 1);
                                   Build tmp=new Build();
                                    tmp.items.add(i0);
                                    tmp.items.add(i1);
                                    tmp.items.add(i2);
                                    tmp.items.add(i3);
                                    tmp.items.add(i4);
                                    tmp.items.add(i5);
                                    /*boolean notsamebuild=true;
                                    int count;
                                    for (count=0;count<builds.size() && notsamebuild;count++) {
                                        if (testIsSameBuild(builds.get(count), tmp)) {
                                                notsamebuild=false;
                                                break;
                                        }
                                    }
                                    if (notsamebuild)*/
                                    /*if (!dispoBuilds.add(tmp)) System.out.println("bug");
                                    if (dispoBuilds.size()>=HOWMUCH) {
                                            synchronized(mutex) {
                                            mutex.notify();
                                                try {
                                                    mutex.wait();
                                                } catch (InterruptedException ex) {
                                                    Logger.getLogger(LinearCompositionThread.class.getName()).log(Level.SEVERE, null, ex);
                                                }
                                        }
                                    }*/
                                    dispoBuilds.add(tmp);
                                    tmp=null;
                                }
                            }
                        }
                    }
                }
            }
            i++;
            System.out.println(""+((((float)i)/size)*100)+"% done");
            
       }
        try {
            ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("toto.bin"));
            oos.writeObject(dispoBuilds);
            oos.flush();
            oos.close();
        } catch (IOException ex) {
            Logger.getLogger(LinearCompositionThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        
       finished=true;
        synchronized(mutex) {
            mutex.notify();
        }
    }


    public ArrayList<Build> getDispoBuilds() {
        ArrayList<Build> ret;
        synchronized(mutex) {
            while (dispoBuilds.size()==0 && !finished) {
                try {
                    mutex.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(LinearCompositionThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            ret=new ArrayList<Build>(dispoBuilds);
            dispoBuilds=new ArrayList();
            mutex.notify();
        }
        return ret;
    }

    public boolean haveIFinished() {
        synchronized(mutex) {
            //System.out.println("he is : "+finished);
            return finished;
        }
    }
}
